package com.keeley.spider.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Tianjun
 * Date: 14-9-15
 * Time: 上午11:15
 * To change this template use File | Settings | File Templates.
 */
public class ApkUtil {
    public static Map<String, String> getApkInfo(String filePath) throws IOException {

        Map<String,String> map = new HashMap<String, String>();

        String command = "aapt dump badging "+filePath;
        Process process= Runtime.getRuntime().exec(command);
        BufferedReader reader= new BufferedReader(new InputStreamReader(process.getInputStream(),"UTF-8"));

        String line;
        StringBuilder result = new StringBuilder(100);
        while ((line=reader.readLine())!=null){
            result.append(line+"\n");
        }
        String reg = "\\s(.+?)[=|:]'(.*?)'";
        Matcher matcher = Pattern.compile(reg).matcher(result.toString());
        while (matcher.find()){
            String key = matcher.group(1);
            if(map.get(key) == null){
                map.put(key,matcher.group(2));
            }
        }
        return map;
    }

    public static List<String> getText(String fileName){
        List<String > list = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader( new FileReader(fileName));
            String name=null;
            while( (name=reader.readLine() )!=null) {
                list.add(name.trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
